package com.fate.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fate.anno.Log;
import com.fate.domain.SysUser;
import com.fate.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.omg.IOP.ServiceContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "后台用户的接口")
@RequestMapping("sys/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 获取当前登录用户的信息
     *
     * @return
     */
    @GetMapping("info")
    @ApiOperation(value = "获取当前登录用户的信息")
    @Log(operation = "全查询角色列表")
    public ResponseEntity<SysUser> getUserInfo(){
        // 获取当前用户
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        SysUser sysUser = sysUserService.getById(Long.valueOf(userId));
        return ResponseEntity.ok(sysUser);

    }

    /**
     * 分页查询管理员的接口
     *
     * @param page
     * @param sysUser
     * @return
     */
    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys:user:page')")
    @ApiOperation("分页查询后台管理员的接口")
    @Log(operation = "分页查询后台管理员的接口")
    public ResponseEntity<Page<SysUser>> loadSysUserPage(Page<SysUser> page,SysUser sysUser){
        Page<SysUser> sysUserPage = sysUserService.findSysUserByPage(page,sysUser);
        return ResponseEntity.ok(sysUserPage);
    }

    /**
     * 新增后台管理员
     *
     * @param sysUser
     * @return
     */
    @PostMapping
    @ApiOperation("新增后台管理员")
    @PreAuthorize("hasAuthority('sys:user:save')")
    @Log(operation = "新增后台管理员")
    public ResponseEntity<Void> addSysUser(@RequestBody SysUser sysUser){
        //新增管理员 需要做的这个操作是谁做的
        String createUserId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        sysUser.setCreateUserId(Long.valueOf(createUserId));
        sysUserService.save(sysUser);
        return ResponseEntity.ok().build();

    }


































}
